package StudyCode.summary.zipfile;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ReturnsInputStreamForTheResourceWithTheSpecifiedAbsoluteName {
	public static void main(String[] argv) throws Exception {
		String absoluteName = "E:/Copy/2.txt";
		System.out.println(getResourceAsStream(absoluteName));
	}


	public static InputStream getResourceAsStream(String absoluteName)
			throws IOException {
		String resourceName = absoluteName.startsWith("/") ? absoluteName
				.substring(1) : absoluteName;
		InputStream ins = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream(resourceName);

		return new BufferedInputStream(ins);
	}


	public static InputStream getResourceAsStream(String relativeName,
	                                              Class<?> clazz) throws IOException {
		String newName = getAbsoluteResourceName(relativeName, clazz);
		return getResourceAsStream(newName);
	}


	public static String getAbsoluteResourceName(String name, Class<?> clazz) {
		StringBuilder absName = new StringBuilder(clazz.getPackage()
				.getName().replace('.', '/'));
		absName.append('/');
		absName.append(name);
		return absName.toString();
	}
}
